//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    #region Using directives

    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Win32;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System.Diagnostics;
    using System.Configuration;
    using System.Windows.Input;

    #endregion

    /// <summary>
    /// Interaction logic for PackagingCentralPane.xaml
    /// </summary>
    public partial class PackagingCenterPaneView : UserControl
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="PackagingCentralPaneView"/> class.
        /// </summary>
        public PackagingCenterPaneView()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(this.OnLoaded);

            this.txtUserName.TextChanged += new TextChangedEventHandler(this.OnMandatoryParametersEntered);
            this.txtPassword.PasswordChanged += new RoutedEventHandler(this.OnPasswordChanged);
            this.txtWorkflowImage.TextChanged += new TextChangedEventHandler(this.OnMandatoryParametersEntered);

            this.txtCredits.TextChanged += new TextChangedEventHandler(this.OnCreditTextChanged);
            this.txtAttributions.TextChanged += new TextChangedEventHandler(this.OnAttributeTextChanged);
            this.btnAddCredits.Click += new RoutedEventHandler(this.OnAddCredits);
            this.btnAddAttributions.Click += new RoutedEventHandler(this.OnAddAttributions);
            this.btnBrowseVCard.Click += new RoutedEventHandler(this.OnBrowseVCard);
            this.btnBrowseWorkflowImage.Click += new RoutedEventHandler(this.OnBrowseWorkflowImage);
            this.txtMyExperimentLink.MouseLeftButtonDown += new MouseButtonEventHandler(this.OnMyExperimentLinkClick);
            this.txtMyExperimentLink.KeyDown += new KeyEventHandler(this.OnMyExperimentLinkKeyDown);
        }
        
        #endregion

        #region Public properties

        /// <summary>
        /// Gets or sets the ActivityPerformancePresenter Model to the view's data context.
        /// </summary>
        public PackagingCenterPanePresenter Presenter
        {
            get { return this.DataContext as PackagingCenterPanePresenter; }
            set { this.DataContext = value; }
        }

        /// <summary>
        /// Gets the activity tree view.
        /// </summary>
        /// <value>The activity tree view.</value>
        public TreeView ActivityTreeView
        {
            get
            {
                return this.treeVw;
            }
        }

        /// <summary>
        /// Gets MyExperiment account user name.
        /// </summary>
        public string UserName
        {
            get
            {
                return this.txtUserName.Text.Trim();
            }
        }

        /// <summary>
        /// Gets MyExperiment account password.
        /// </summary>
        public string Password
        {
            get
            {
                return this.txtPassword.Password.Trim();
            }
        }

        /// <summary>
        /// Gets MyExperiment contacts file used.
        /// </summary>
        public string VCardFile
        {
            get
            {
                return this.txtVCard.Text.Trim();
            }
        }

        /// <summary>
        /// Gets the workflow icon.
        /// </summary>
        public string WorkflowIcon
        {
            get
            {
                return this.txtWorkflowImage.Text.Trim();
            }
        }

        /// <summary>
        /// Gets the accessiblity property.
        /// </summary>
        public string Accessibility
        {
            get
            {
                string accessibility = string.Empty;
                ComboBoxItem item = this.cmbAccessibility.SelectedItem as ComboBoxItem;
                if(item != null)
                {
                    accessibility = item.Content.ToString();
                }

                return accessibility;
            }
        }

        /// <summary>
        /// Gets workflow credits.
        /// </summary>
        public Collection<string> Credits
        {
            get
            {
                Collection<string> credits = new Collection<string>();
                foreach (string credit in this.lstCredits.Items)
                {
                    credits.Add(credit);
                }

                return credits;
            }
        }

        /// <summary>
        /// Gets workflow attributions.
        /// </summary>
        public Collection<string> Attributions
        {
            get
            {
                Collection<string> attributions = new Collection<string>();
                foreach (string attribution in this.lstAttributions.Items)
                {
                    attributions.Add(attribution);
                }

                return attributions;
            }
        }

        #endregion

        #region Private event handlers

        /// <summary>
        /// Handles the Loaded event of the PackagingCentralPaneView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.Presenter.GetData.Execute(null);
        }

        /// <summary>
        /// Fired on initialization.
        /// </summary>
        /// <param name="sender">
        /// Param listview.
        /// </param>
        /// <param name="e">
        /// EventArgs.
        /// </param>
        private void OnListViewInitialized(object sender, EventArgs e)
        {
            ListView senderListVw = sender as ListView;
            if (0 == senderListVw.Items.Count)
            {
                senderListVw.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Browses for a V-Card in the filesystem.
        /// </summary>
        /// <param name="sender">
        /// Browse V-Card button.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnBrowseVCard(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "V Cards (*.vcf)|*.vcf";
            if ((bool)dialog.ShowDialog(Application.Current.MainWindow))
            {
                this.txtVCard.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// Browses for a workflow icon in the filesystem.
        /// </summary>
        /// <param name="sender">
        /// Browse Workflow Icon button.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnBrowseWorkflowImage(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "PNG Image (*.png)|*.png|JPEG Image (*.jpg)|*.jpg";
            if ((bool)dialog.ShowDialog(Application.Current.MainWindow))
            {
                this.txtWorkflowImage.Text = dialog.FileName;
            }
        }

        /// <summary>
        /// Fired when text in credits textbox changes.
        /// Enables or disables Add button accordingly.
        /// </summary>
        /// <param name="sender">
        /// Credits textbox.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnCreditTextChanged(object sender, TextChangedEventArgs e)
        {
            string credit = this.txtCredits.Text.Trim();
            if (credit.Length > 0 && !this.lstCredits.Items.Contains(credit))
            {
                this.btnAddCredits.IsEnabled = true;
            }
            else
            {
                this.btnAddCredits.IsEnabled = false;
            }
        }

        /// <summary>
        /// Fired when text in attributions textbox changes.
        /// Enables or disables Add button accordingly.
        /// </summary>
        /// <param name="sender">
        /// Attributions textbox.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnAttributeTextChanged(object sender, TextChangedEventArgs e)
        {
            string attribution = this.txtAttributions.Text.Trim();
            if (attribution.Length > 0 && !this.lstAttributions.Items.Contains(attribution))
            {
                this.btnAddAttributions.IsEnabled = true;
            }
            else
            {
                this.btnAddAttributions.IsEnabled = false;
            }
        }

        /// <summary>
        /// Adds the credit entered into credits list.
        /// </summary>
        /// <param name="sender">
        /// Add credits button.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnAddCredits(object sender, RoutedEventArgs e)
        {
            this.lstCredits.Items.Add(this.txtCredits.Text.Trim());
            this.txtCredits.Text = string.Empty;
            this.txtCredits.Focus();
        }

        /// <summary>
        /// Adds the attribution entered into attributions list.
        /// </summary>
        /// <param name="sender">
        /// Add attribution button.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnAddAttributions(object sender, RoutedEventArgs e)
        {
            this.lstAttributions.Items.Add(this.txtAttributions.Text.Trim());
            this.txtAttributions.Text = string.Empty;
            this.txtAttributions.Focus();
        }

        /// <summary>
        /// Launches myExperiment site.
        /// </summary>
        /// <param name="sender">
        /// myExperiment hyperlink.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnMyExperimentLinkClick(object sender, MouseButtonEventArgs e)
        {
            this.LaunchSite(ConfigurationManager.AppSettings["MyExperimentSite"]);
        }

        /// <summary>
        /// Launches myExperiment site.
        /// </summary>
        /// <param name="sender">
        /// myExperiment hyperlink.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnMyExperimentLinkKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Space)
            {
                this.LaunchSite(ConfigurationManager.AppSettings["MyExperimentSite"]);
            }
        }

        /// <summary>
        /// Checks if all mandatory parameters are entered or not.
        /// If yes, enables Upload button. Otherwise disables it.
        /// </summary>
        /// <param name="sender">
        /// Mandatory parameter fields.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            this.ValidateInputs();
        }

        /// <summary>
        /// Checks if all mandatory parameters are entered or not.
        /// If yes, enables Upload button. Otherwise disables it.
        /// </summary>
        /// <param name="sender">
        /// Mandatory parameter fields.
        /// </param>
        /// <param name="e">
        /// Attributes of the event.
        /// </param>
        private void OnMandatoryParametersEntered(object sender, TextChangedEventArgs e)
        {
            this.ValidateInputs();
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Checks if all mandatory parameters are entered or not.
        /// If yes, enables Upload button. Otherwise disables it.
        /// </summary>
        private void ValidateInputs()
        {
            this.btnUpload.IsEnabled = false;
            string username = this.txtUserName.Text.Trim();
            if (username.Length > 0)
            {
                string password = this.txtPassword.Password.Trim();
                if (password.Length > 0)
                {
                    string workflowImage = this.txtWorkflowImage.Text.Trim();
                    if (workflowImage.Length > 0)
                    {
                        this.btnUpload.IsEnabled = true;
                    }
                }
            }
        }

        /// <summary>
        /// Launches the site specified by url.
        /// </summary>
        /// <param name="url">
        /// Url of the site.
        /// </param>
        private void LaunchSite(string url)
        {
            try
            {
                Process.Start(url);
            }
            catch (Exception ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.Message);
            }
        }

        #endregion
    }
}
